package com.drpclub.gamebackend.controller;

import com.drpclub.gamebackend.bean.ContentPage;
import com.drpclub.gamebackend.common.constant.ActionUri;
import com.drpclub.gamebackend.common.constant.RootUri;
import com.drpclub.gamebackend.common.utils.DrpUtils;
import com.drpclub.gamebackend.common.utils.NetworkResult;
import com.drpclub.gamebackend.common.utils.log.RsHelper;
import com.drpclub.gamebackend.service.RecommendResourceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.Callable;

import static com.drpclub.gamebackend.common.utils.DrpUtils.getIpAddr;

/**
 * @author ：drp
 * @description：推荐内容
 * @date ：Created in 2020/5/8 17:36
 */
@RequestMapping(RootUri.RECOMMEND)
@RestController
public class RecommendResourceController {

    private final RecommendResourceService recService;

    @Autowired
    public RecommendResourceController(RecommendResourceService recService) {
        this.recService = recService;
    }

    @ApiOperation(value = "horizontal carousel")
    @GetMapping(ActionUri.CAROUSEL)
    public Callable<NetworkResult<Object>> getCarousel(@RequestParam Integer page,
                                                       @RequestParam Integer size,
                                                       @RequestParam(required = false) String type) {
        return () -> RsHelper.success(recService.getCarousel(page, size, type));
    }

    @ApiOperation(value = "get content")
    @PostMapping(ActionUri.CONTENT)
    public Callable<NetworkResult<Object>> getContentBody(@RequestBody @Valid ContentPage contents) {
        return () -> RsHelper.success(recService.getContent(
                contents.getPage(), contents.getSize(), contents.getTypes())
        );
    }

    @ApiOperation(value = "count content type")
    @GetMapping(ActionUri.COUNT_TYPE)
    public Callable<NetworkResult<Object>> countContentType() {
        return () -> RsHelper.success(recService.countContentType());
    }

    @ApiOperation(value = "get recommend body")
    @GetMapping(ActionUri.BODY)
    public Callable<NetworkResult<Object>> getRecommendBody(@RequestParam Integer size, @RequestParam Integer page) {
        return () -> RsHelper.success(recService.getRecommendBody(size, page));
    }

    @ApiOperation(value = "热度更新")
    @PutMapping(ActionUri.CHANGE_HEAT)
    public Callable<NetworkResult<Object>> changeHeat(String contentId, String username, HttpServletRequest request) {
        return () -> RsHelper.success(recService.changeHeat(contentId, username, getIpAddr(request)));
    }
}
